範例如下:
程式碼:
//
// ViewController.swift
// Dog Breed Recognition App
//
// Created by PecaJo on 2021/6/24.
import UIKit
import FirebaseStorage
import FirebaseDatabase
class ViewController: UIViewController {
@IBOutlet weak var uploadButton: UIButton!
@IBOutlet weak var libraryButton: UIButton!
@IBOutlet weak var imageview: UIImageView!
@IBOutlet weak var dogNameLabel: UILabel!
@IBOutlet weak var dogAccLabel: UILabel!
@IBOutlet weak var loadingLabel: UILabel!
let imagePicker = UIImagePickerController()
var ref: DatabaseReference! = Database.database().reference()
var tmpBool = false
var detect = "False"
override func viewDidLoad() {
super.viewDidLoad()
self.imagePicker.delegate = self
DispatchQueue.global().async {
while(true){
sleep(1)
if self.tmpBool {
self.uploadasync()
}
}
}
}
@IBAction func clickuploadbutton(_ sender: Any) {
if imageview.image != nil {
self.loadingLabel.isHidden = false
uploadFile()
ref.child("DetectBool/Bool/").setValue("True")
ref.child("dogName/Name/").setValue("")
ref.child("dogAcc/Acc/").setValue("")
tmpBool = true
}
else{
let alertController = UIAlertController(title: "圖片訊息", message: "請放入圖片", preferredStyle: UIAlertController.Style.alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
present(alertController, animated: true, completion: nil)
}
}
@IBAction func clicklibrarybutton(_ sender: Any) {
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true)
}
func uploadFile(){
let url = URL(string: "https://XXXXXXXXXXXXXXXX")
// Create a root reference
let storageRef = Storage.storage().reference()
// Data in memory
let imageData: Data = imageview.image!.jpegData(compressionQuality: 1.0)!
// Create a reference to the file you want to upload
let riversRef = storageRef.child("dog.jpg")
// Upload the file to the path "images/rivers.jpg"
let uploadTask = riversRef.putData(imageData, metadata: nil) { (metadata, error) in
guard let metadata = metadata else {
// Uh-oh, an error occurred!
return
}
// Metadata contains file metadata such as size, content-type.
let size = metadata.size
// You can also access to download URL after upload.
riversRef.downloadURL { (url, error) in
guard let downloadURL = url else {
// Uh-oh, an error occurred!
return
}
}
}
}
func uploadasync(){
DispatchQueue.main.async { [self] in
self.ref.child("DetectBool/Bool/").observeSingleEvent(of: .value) {
(snapshot) in
self.detect = (snapshot.value as? String)!
print(self.detect)
}
if self.dogNameLabel.text != "" {
if self.dogAccLabel.text != "" {
if self.detect == "False" {
self.loadingLabel.isHidden = true
}
}
}
self.ref.child("dogName/Name/").observeSingleEvent(of: .value) {
(snapshot) in
if let name = snapshot.value as? String {
self.dogNameLabel.text = "\(name)"
}
}
self.ref.child("dogAcc/Acc/").observeSingleEvent(of: .value) {
(snapshot) in
if let acc = snapshot.value as? String {
self.dogAccLabel.text = "\(acc)"
}
}
}
}
}
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
imageview.contentMode = .scaleAspectFit
imageview.image = pickedImage
imageview.layer.masksToBounds = true;
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}
其餘功能會在之後繼續增加
對於自己30天的紀錄與挑戰
到這告一段落
未來還會繼續增進自己的能力
謝謝觀看